﻿using CreateDefect.Domain;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CreateDefect.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Login to Quality Center to submit a Defect.";

            if (Session["QCConnection"] != null)
            {
                QCConnection connection = (QCConnection)Session["QCConnection"];
                if (connection.ServerConnected == true)
                    ViewBag.Connected = true;
                if (connection.ProjectConnected == true)
                    return RedirectToAction("Index");
            }

            return View();
        }

        /// <summary>
        /// Authenticates the user to quality center
        /// </summary>
        /// <param name="username">username as string</param>
        /// <param name="password">password as string</param>
        public void Authenticate(string username, string password)
        {
            try
            {
                QCConnection connection = new QCConnection();
                connection.Authenticate("https://hpalmprod.apollogrp.edu/qcbin/", username, password);

                Session["QCConnection"] = connection;
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Returns the domains visible to the user
        /// </summary>
        /// <returns>list of domains as REST</returns>
        public object Domains()
        {
            try
            {
                QCConnection connection = (QCConnection)Session["QCConnection"];
                return JsonConvert.SerializeObject(connection.Domains());
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Returns the projects for the connected user
        /// </summary>
        /// <param name="domain">the domain for which the user wants the projects</param>
        /// <returns>list of projects as REST</returns>
        public object Projects(string domain)
        {
            try
            {
                QCConnection connection = (QCConnection)Session["QCConnection"];
                return JsonConvert.SerializeObject(connection.Projects(domain));
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Logs into quality center from the UI
        /// </summary>
        /// <param name="domain">domain to connect to</param>
        /// <param name="project">project to connect to</param>
        public void QCLogin(string domain, string project)
        {
            try
            {
                QCConnection connection = (QCConnection)Session["QCConnection"];
                connection.Login(domain, project);
                Session["QCConnection"] = connection;
            }
            catch (Exception ex) { throw ex; }
        }
    }
}
